#include "gui/itemviewreportsdialog.h"
#include "gui/separatorwidget.h"
#include "core/constants.h"
#include "core/itemcontroller.h"
#include "core/itemmodel.h"

void ItemViewReportsDialog::initUiComponents()
{
    QFont font;
    mainLayout = new QVBoxLayout;
    headerLabel = new QLabel("Item Reports");
    font = headerLabel->font();
    font.setPointSize(font.pointSize() + HEADER_UPSIZE);
    headerLabel->setFont(font);
    mainLayout->addWidget(headerLabel);

    subHeaderLabel = new QLabel("View important information about your Items");
    mainLayout->addWidget(subHeaderLabel);

    mainLayout->addWidget(new SeparatorWidget);

    topProductReport = new CollapsibleWidget;
    topProductReport->setText("Top Products for the Month");
    topProductList = new ShinyList;
    topProductReport->setWidget(topProductList);
    mainLayout->addWidget(topProductReport);

    dwindlingStockReport = new CollapsibleWidget;
    dwindlingStockReport->setText("Product with Dwindling Stocks");
    dwindlingStockList = new ShinyList;
    dwindlingStockReport->setWidget(dwindlingStockList);
    mainLayout->addWidget(dwindlingStockReport);

    mainLayout->addStretch();

    mainLayout->addWidget(new SeparatorWidget);
    buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
    mainLayout->addWidget(buttonBox);

    this->setLayout(mainLayout);
}

void ItemViewReportsDialog::initSignalsAndSlots()
{
    connect(this->buttonBox, SIGNAL(rejected()),
            this, SLOT(reject()));
}

void ItemViewReportsDialog::generateTopProductReport()
{
    ItemController ctrl;
    QDate month;
    QList<BaseModel *> models = ctrl.getBestsellersForMonth(month);
    ItemModel *item;
    ShinyListItem *listItem;
    int i;

    for (i = 0; i < models.length(); i++) {
        listItem = new ShinyListItem;
        item = (ItemModel *) models.at(i);
        listItem->setMainText(QString("%1 %2").arg(item->getName(), item->getCode()));
        listItem->setSubText("8 sold");
        topProductList->addItem(listItem);
    }
}

void ItemViewReportsDialog::generateDwindlingStockReport()
{
    ItemController ctrl;
    QDate month;
    QList<BaseModel *> models = ctrl.getAllDwindling();
    ItemModel *item;
    ShinyListItem *listItem;
    int i;

    for (i = 0; i < models.length(); i++) {
        listItem = new ShinyListItem;
        item = (ItemModel *) models.at(i);
        listItem->setMainText(QString("%1 %2").arg(item->getName(), item->getCode()));
        listItem->setSubText(QString::number(item->getNumStocks()) + " stock/s left");
        dwindlingStockList->addItem(listItem);
    }
}

ItemViewReportsDialog::ItemViewReportsDialog(QWidget *parent) :
    QDialog(parent)
{
    this->setWindowTitle("Item Reports");
    this->setMinimumSize(400, 300);
    this->initUiComponents();
    this->initSignalsAndSlots();
    this->generateTopProductReport();
    this->generateDwindlingStockReport();
}
